package cn.singno.bob.common.bean;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

import cn.singno.bob.constant.Chartset;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

public class HttpClientHelper {

	private String url;
	
	private Map<String, String> headers;
	
	private Map<String, Object> params;
	
	private String encoding = Chartset.UTF8;
	
	private HttpGet httpGet;
	
	private HttpPost httpPost;
	
	private Map<String, File> file;
	
	private List<Map<String, File>> files;
	
	
	
	public HttpClientHelper(String url, Map<String, Object> params) {
		super();
		this.url = url;
		this.params = params;
	}

	
	
	
	public String getUrl() {
		return url;
	}
	
	public HttpGet getHttpGet() {
		this.httpGet = new HttpGet( url + (this.params == null ? "" : joinParamter(this.params) ) );
		if(MapUtils.isNotEmpty(this.headers)){
			this.httpGet.setHeaders(this.getHeaders());
		}
		return  this.httpGet;
	}
	
	public HttpPost getSimpleHttpPost() throws UnsupportedEncodingException{
		this.httpPost = new HttpPost(this.url);
		 //设置需要提交的参数
        List<NameValuePair> list  = new ArrayList<NameValuePair>();
        for (String key : params.keySet()) {
        	Object obj = params.get(key);
        	if(obj != null){
        		list.add(new BasicNameValuePair(key, JSON.toJSONString(obj)));
        	}
        }
        this.httpPost.setEntity(new UrlEncodedFormEntity(list, this.encoding));;
        if(MapUtils.isNotEmpty(this.headers)){
			this.httpPost.setHeaders(this.getHeaders());
		}
		return this.httpPost;
	}
	
	public HttpPost getFileHttpPost(){
		this.httpPost = new HttpPost(this.url);
		HttpEntity entity = setFileParamer(setFile(MultipartEntityBuilder.create())).build();
		this.httpPost.setEntity(entity);
		if (MapUtils.isNotEmpty(this.headers)) {
			this.httpPost.setHeaders(this.getHeaders());
		}
		return this.httpPost;
	}
	
	/**
	 * 设置文件上传的文件流参数
	 * @param multipartEntityBuilder
	 * @return
	 */
	private MultipartEntityBuilder setFile(MultipartEntityBuilder multipartEntityBuilder){
		if(MapUtils.isNotEmpty(file)){
			if(CollectionUtils.isEmpty(files)){
				this.files = Lists.newArrayList();
			}
			files.add(file);
		}
		for (Map<String, File> map : files) {
			for (String key : map.keySet()) {
				multipartEntityBuilder.addBinaryBody(key, map.get(key));
			}
		}
		return multipartEntityBuilder;
	}
	
	/**
	 * 设置文件上传的基本参数
	 * @param multipartEntityBuilder
	 * @return
	 */
	private MultipartEntityBuilder setFileParamer(MultipartEntityBuilder multipartEntityBuilder){
		if(MapUtils.isNotEmpty(params)){
			for (String key : params.keySet()) {
				Object obj = params.get(key);
				if(obj != null){
					multipartEntityBuilder.addTextBody(key, JSON.toJSONString(obj));
				}
			}
		}
		return multipartEntityBuilder;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Header[] getHeaders() {
		return joinHeader(this.headers);
	}

	public HttpClientHelper setHeaders() {
		
		return this;
	}
	
	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	
	
	 //这是组装参数
    public String joinParamter(Map<String, Object> parameters){
        String para = "?";
        for (String str :parameters.keySet()) {
            para += str + "=" + parameters.get(str) + "&";
        }
        return para.substring(0, para.length()-1);
    }
	
	 //这是组装头部
    public  Header[] joinHeader(Map<String,String> headers){
        Header[] allHeader= new BasicHeader[headers.size()];
        int i  = 0;
        for (String str :headers.keySet()) {
            allHeader[i] = new BasicHeader(str,headers.get(str));
            i++;
        }
        return allHeader;
    }
	
}
